package com.thinkgem.jeesite.modules.data.rep.web;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.data.rep.entity.TfEcReportConfig;
import com.thinkgem.jeesite.modules.data.rep.service.ReportConfService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 报表配置controller
 *
 * @Author Havel
 */
@Controller
@RequestMapping(value = "${adminPath}/report/config")
public class ReportConfController extends ReportConfBaseController {

    @Autowired
    private ReportConfService reportConfService;

    @RequiresPermissions("report:config:view")
    @RequestMapping(value = {"list", ""})
    public String list(TfEcReportConfig tfEcReportConfig, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<TfEcReportConfig> page = tfEcReportConfig.getPage();
        page.setPageNo(getPageNo(request, response));
        page.setPageSize(getPageSize(request, response, 10));
        page = reportConfService.findPage(page, tfEcReportConfig);
        model.addAttribute("page", page);
        return "modules/data/rep/reportList";
    }

    @RequiresPermissions("report:config:edit")
    @RequestMapping(value = "form")
    public String form(String reportId, Model model) {
        TfEcReportConfig report = new TfEcReportConfig();
        if (!StringUtils.isEmpty(reportId)) {
            report = reportConfService.getConfigById(Long.parseLong(reportId));
        }
        model.addAttribute("report", report);
        return "modules/data/rep/reportForm";
    }

    @RequiresPermissions("report:config:edit")
    @RequestMapping(value = "save")
    public String save(TfEcReportConfig report, HttpServletRequest request, Model model, RedirectAttributes redirectAttributes) {
        if (beanValidator(model, report)) {
            report.setQueryKey(cleanXssFilter(StringEscapeUtils.unescapeHtml4(report.getQueryKey())));
            report.setSqlRef(cleanXssFilter(StringEscapeUtils.unescapeHtml4(report.getSqlRef())));
            report.setProce(cleanXssFilter(StringEscapeUtils.unescapeHtml4(report.getProce())));

            User currentUser = UserUtils.getUser();
            report.setUpdateStaffId(currentUser.getPhone());
            reportConfService.saveAndUpdate(report);
            this.addMessage(redirectAttributes, "保存" + StringUtils.abbr(report.getReportName(), 50) + "成功");
        } else {
            return form(report.getReportId().toString(), model);
        }
        return "redirect:" + adminPath + "/report/config";
    }

    @RequiresPermissions("report:config:edit")
    @RequestMapping(value = "delete")
    public String delete(@RequestParam long reportId, Model model, RedirectAttributes redirectAttributes) {
        TfEcReportConfig report = reportConfService.getConfigById(reportId);
        report.setDelFlag(TfEcReportConfig.DEL_FLAG_DELETE);
        report.setUpdateStaffId(UserUtils.getUser().getPhone());
        reportConfService.delete(report);
        this.addMessage(redirectAttributes, "删除" + StringUtils.abbr(report.getReportName(), 50) + "成功");
        return "redirect:" + adminPath + "/report/config/";
    }
}
